using System;
using System.Text;

namespace KodeIT.Web
{
    internal delegate void NativeGetter();

    internal class NativeString
    {
        private StringBuilder _builder;
        private int _nativeLength;
        private string _data;
        private bool _setted;
        private bool _getted;
        private NativeGetter _getter;

        internal NativeString(int nativeLength, NativeGetter getter)
        {
            _nativeLength = nativeLength;
            _getter = getter;
        }

        internal int NativeLength
        {
            get { return _nativeLength; }
        }

        internal StringBuilder Builder
        {
            get
            {
                if (!_setted)
                {
                    if (null == _builder)
                    {
                        _builder = new StringBuilder(_nativeLength);
                    }
                }

                return _builder;
            }
        }

        internal string Data
        {
            get
            {
                if (!_setted)
                {
                    if (!_getted)
                    {
                        _getter();
                        _data = _builder.ToString();
                        _getted = true;
                    }
                }

                return _data;
            }
            set
            {
                _setted = true;
                _data = value;
            }
        }

        internal bool Setted
        {
            get { return _setted; }
        }
    }
}
